package cn.zyx.quartz.service.impl;

import cn.zyx.basic.util.QuartzUtils;
import cn.zyx.quartz.domain.QuartzJobInfo;
import cn.zyx.quartz.job.MainJob;
import cn.zyx.quartz.service.IQuartzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class QuartzServiceImpl implements IQuartzService {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @Override
    public void addQuartz(QuartzJobInfo quartzJobInfo) {
        try {
            QuartzUtils.addJob(schedulerFactoryBean.getScheduler(),
                    quartzJobInfo.getJobName(),
                    MainJob.class,
                    quartzJobInfo.getParams(),
                    quartzJobInfo.getCronj());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void removeQuartz(String unionPaySn) {
        QuartzUtils.removeJob(schedulerFactoryBean.getScheduler(),unionPaySn);
    }
}
